package com.lucy.rabc.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Validator;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.rabc.entity.SysDept;
import com.lucy.rabc.entity.SysMenu;
import com.lucy.rabc.params.MenuCreatParams;
import com.lucy.rabc.params.MenuQueryParams;
import com.lucy.rabc.params.MenuUpdateParams;
import com.lucy.rabc.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author kindear
 */
@Slf4j
@RestController
@RequestMapping("system")
@Api(tags = "菜单管理")
public class MenuController {
    @Autowired
    SysMenuService menuService;

    @PostMapping("menus")
    @ApiOperation(value = "菜单新建",notes = "")
    public ResponseDTO<Object> MenuCreate(@RequestBody @Validated MenuCreatParams params){
        Long parentId = params.getParentId();
        Optional<SysMenu> parentOp = menuService.getOne(parentId);
        SysMenu menu = new SysMenu();
        if (parentOp.isPresent()){
            BeanUtil.copyProperties(params,menu, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
            menu.setParent(parentOp.get());
            menu.setParentName(parentOp.get().getMenuName());
            return ResponseDTO.succData(
                    menuService.saveOne(menu)
            );
        }else {
            BeanUtil.copyProperties(params,menu, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
            return ResponseDTO.succData(
                    menuService.saveOne(menu)
            );
        }
    }

    @DeleteMapping("menus/{menuId}")
    @ApiOperation(value = "菜单删除",notes = "")
    public ResponseDTO<Object> MenuDelete(@PathVariable("menuId") Long menuId){
        menuService.deleteOne(menuId);
        return ResponseDTO.succMsg("删除成功");
    }

    @PutMapping("menus")
    @ApiOperation(value = "菜单更新",notes = "")
    public ResponseDTO<Object> MenuUpdate(@RequestBody @Validated MenuUpdateParams params){
        Long menuId = params.getId();
        Optional<SysMenu> menuOp = menuService.getOne(menuId);
        if (!menuOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        SysMenu menu = menuOp.get();
        BeanUtil.copyProperties(params,menu,CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
        return ResponseDTO.succData(
                menuService.updateOne(menu)
        );
    }

    @GetMapping("menus")
    @ApiOperation(value = "菜单列表",notes = "")
    public ResponseDTO<Object> MenuListQuery(MenuQueryParams params){
        Sort sort;
        if(params.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);
        /*
        构建 Example
         */
        SysMenu menu = new SysMenu();
        BeanUtil.copyProperties(params,menu,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<SysMenu> example = Example.of(menu);
        return ResponseDTO.succData(menuService.getAll(pageable,example));
    }

    @GetMapping("menus/{menuId}")
    @ApiOperation(value = "菜单查询",notes = "")
    public ResponseDTO<Object> MenuQuery(@PathVariable("menuId") Long menuId){
        Optional<SysMenu> menuOp = menuService.getOne(menuId);
        if (!menuOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        SysMenu menu = menuOp.get();
        return ResponseDTO.succData(menu);
    }

    @GetMapping("menus/tree")
    @ApiOperation(value = "菜单选择树",notes = "")
    public ResponseDTO<Object> buildMenuTree(){
        SysMenu menu = new SysMenu();
        menu.setParent(null);
        Example<SysMenu> example = Example.of(menu);
        List<SysMenu> menus = menuService.getExample(example);
        menus = menus.stream().filter(sysMenu -> Validator.isEmpty(sysMenu.getParent())).collect(Collectors.toList());
        return ResponseDTO.succData(menus);
    }
}
